package com.jss.lanouhn.fristapp.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.adapters.FreeRightSmallAdapter;
import com.jss.lanouhn.fristapp.modles.FreeFragmentRightInfo;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/9/3.
 */
public class HomeSmallFreeFragmentOther extends Fragment{
    private FreeRightSmallAdapter adapter;
    private RecyclerView recyclerView;
    private FreeFragmentRightInfo info;
    private String url="http://api.liwushuo.com/v2/daily_lucky/past_item_awards?limit=20&offset=0";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_home_free_rigth,container,false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView= (RecyclerView) getView().findViewById(R.id.rv_fragment_free_right);
        LinearLayoutManager manager=new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        initData();
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client=new OkHttpClient();
                Request request=new Request.Builder()
                        .url(url)
                        .build();
                Call call=client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result=response.body().string();
                        Gson gson=new Gson();
                        info=gson.fromJson(result,FreeFragmentRightInfo.class);
                        handler.sendEmptyMessage(1);
                    }
                });
            }
        }).start();
    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
              adapter=new FreeRightSmallAdapter(getActivity(),info);
                recyclerView.setAdapter(adapter);
            }
        }
    };
}

